﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApi.Dict.Entitys;
using RuoVea.OmiApi.Dict.Service;

namespace RuoVea.Stocks.Server.Service;

[ApiExplorerSettings(GroupName = "Stocks")]
public partial class RecordService : IApplicationService
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<StockContent> _thisRepository;
    private readonly SysDictTypeService _sysDictTypService;
    private readonly ICurrentUser _currentUser;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param>
    /// <param name="sysDictTypService"></param>
    /// <param name="currentUser"></param>
    public RecordService(SugarRepository<StockContent> thisRepository, SysDictTypeService sysDictTypService, ICurrentUser currentUser)
    {
        _thisRepository = thisRepository;
        _sysDictTypService = sysDictTypService;
        _currentUser = currentUser;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<RestfulResult<PageResult<StockContent>>> GetPagesAsync(RecordsPageParam? data)
    {
        var restful = new RestfulResult<PageResult<StockContent>>() { Code = CodeStatus.OK };
        data ??= new RecordsPageParam { PageNo = 1, PageSize = 10 };

        var query = await _thisRepository.AsQueryable()
            .Where(u => u.Creator == _currentUser.UserId)
            .WhereIF(data.CategoryId > 0, u => u.CategoryId == data.CategoryId)
            .WhereIF(data.Purchase > 0, u => u.Purchase == data.Purchase)
            .OrderByDescending(x => x.RecordTime).OrderByDescending(x => x.Sort)
            .ToPageAsync(data.PageNo, data.PageSize);

        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task<RestfulResult> AddAsync(StockContent data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.RecordTime == data.RecordTime && u.Lots == data.Lots && u.Creator == _currentUser.UserId);
        if (isExist)
        {
            restful.Message = "标题重复";
            return restful;
        }
        data.Creator = _currentUser.UserId;
        bool result = await _thisRepository.InsertAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "添加失败";
            return restful;
        }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<RestfulResult> UpdateAsync(StockContent data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.Id == data.Id && u.Creator == _currentUser.UserId);
        if (!isExist)
        {
            restful.Message = "记录不存在";
            return restful;
        }
        bool result = await _thisRepository.UpdateAsync(data, true, true, "笔记修改");
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<RestfulResult> DeleteAsync(EntityBaseId data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        if (data.Id == 0)
        {
            restful.Message = "删除失败";
            return restful;
        }
        bool isDelete = await _thisRepository.DeleteByIdAsync(data.Id);
        if (isDelete)
        {
            restful.Data = isDelete;
            restful.Message = "删除成功";
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }

    /// <summary>
    /// 获取分类
    /// </summary>
    /// <returns></returns>
    public async Task<RestfulResult<object>> GetCategoryAsync()
    {
        RestfulResult<object> restful = new() { Code = CodeStatus.OK };
        List<SysDictValue> dictValues = await _sysDictTypService.GetDictValuesByTypeCodeAsync("stocks_records");
        if (dictValues == null || dictValues.Count == 0)
        {
            restful.Code = CodeStatus.BadRequest;
            restful.Message = "请先股票配置";
            return restful;
        }
        restful.Data = dictValues.Select(x => new { label = x.Label, value = x.Id }).ToList();
        return restful;
    }
}